const logService = require('../services/logService');
const { success, successWithData, error } = require('../utils/resultwrapper');

class LogController {
    /**
     * 创建系统日志
     */
    async createLog(req, res) {
        try {
            const logData = req.body;
            const result = await logService.createLog(logData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 删除系统日志
     */
    async deleteLog(req, res) {
        try {
            const { id } = req.params;
            const result = await logService.deleteLog(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 批量删除系统日志
     */
    async batchDeleteLog(req, res) {
        try {
            const { ids } = req.body;
            const result = await logService.batchDeleteLog(ids);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取系统日志详情
     */
    async getLogById(req, res) {
        try {
            const { id } = req.params;
            const result = await logService.getLogById(id);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取系统日志列表
     */
    async getLogList(req, res) {
        try {
            const result = await logService.getLogList(req.query);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 清空系统日志
     */
    async clearLog(req, res) {
        try {
            const result = await logService.clearLog();
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取访问趋势统计
     */
    async getVisitTrend(req, res) {
        try {
            const { startDate, endDate } = req.query;
            if (!startDate || !endDate) {
                return res.json(error('请提供开始日期和结束日期'));
            }
            const result = await logService.getVisitTrend(startDate, endDate);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }
}

module.exports = new LogController();